package com.stone.business.jc.action;

/**
 * <pre>
 * 周工作计划
 * @author
 * </pre>
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

import com.stone.business.jc.pojo.TPlanWeek;
import com.stone.business.jc.service.JcCommonService;
import com.stone.para.IConstant;
import com.stone.sys.pojo.TOrgan;
import com.stone.sys.pojo.TUser;
import com.stone.utils.JsonUtils;
import com.stone.utils.StringUtils;

import frm.ssh.action.BaseAction;

public class PlanWeekListAction extends BaseAction {

	private static final long serialVersionUID = 3979577965804736350L;

	private JcCommonService myService;
	
	public JcCommonService getMyService() {
		return myService;
	}

	public void setMyService(JcCommonService myService) {
		//this.service = myService;
		this.myService = myService;
	}
	
//    /** * 构造方法. */
//    public PlanWeekListAction() {
//        this.entityClass = TPlanWeek.class;
//    }
    
    String bz;
    
    String isAdmin;
    
    String week;
    
    String currentday;
    
    String weekid;

	/**
	 * 设置查询条件
	 */
	public HashMap<String, Object> setSearchCondition() {
		
		HashMap<String, Object> m = new HashMap<String, Object>();
		
		List<Object> valsList = new ArrayList<Object>();
		
		m.put("cols", "new map(s.id as id, to_char(s.planweekstart,'YYYY-MM-DD') || '~' || to_char(s.planweekend,'YYYY-MM-DD') as planweek)");

		String conditions = " 1=1";
		
		// 开始时间
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon2())) {
			conditions += " AND TO_CHAR(s.planweekstart,'yyyy-MM-dd') >= ?";
			valsList.add(super.getSearchBean().getCon2());
		}
		
		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon3())) {
			conditions += " AND TO_CHAR(s.planweekstart,'yyyy-MM-dd') <= ?";
			valsList.add(super.getSearchBean().getCon3());
		}
		
//		// 变电站
//		if (!StringUtils.isNullOrBlankFull(super.getSearchBean().getCon4())) {
//			conditions += " AND s.bdzname like ?";
//			valsList.add("%" + super.getSearchBean().getCon4()+ "%");
//		}

		// 查询条件
		m.put("conditions",conditions);
		m.put("clause", " order by s.planweekstart Desc");
		m.put("alias", "s");//, TOrgan o
		m.put("vals", valsList.toArray());


		TUser user = (TUser) super.getSession().getAttribute(IConstant.SESSION_TUSER);
		String roles = (String) super.getSession().getAttribute(IConstant.SESSION_ROLES);
		/*// 安全员
		if ("006".equals(user.getJxkhgw())) {
			
			this.bz = "安全员";
			
			// 技术员
		} else if ("004".equals(user.getJxkhgw())) {
			
			this.bz = "技术员";
			
			// 操作队队长
		} else if ("005".equals(user.getJxkhgw())) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 维修班长
		} else if ("007".equals(user.getJxkhgw())) {
			
			this.bz = "维修班长";
			
		} else {
			this.bz = "";
		}*/
     if (roles.contains("ZBZ")) {
			
			this.bz =((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			// 专工
		} else if (roles.contains("gqgl")) {
			
			this.bz = "gqgl";
			
			// 班组管理
		} else if (roles.contains("bzgl")) {
			
			this.bz = ((TOrgan) super.getSession().getAttribute(IConstant.SESSION_TORGAN)).getOrganName();
			
			
		}  else {
			this.bz = "";
		}
		isAdmin = (String) super.getSession().getAttribute(IConstant.SESSION_ISADMIN);
		
		return m;
		
	}
	
	public void getWeekDate() throws Exception {
		
		List<HashMap<String,Object>> listTree;
		
		// 左侧的树
		if ("-1".equals(week)) {
			
			listTree = myService.findListBySql("select to_char(planweekstart,'YYYY') as text, to_char(planweekstart,'YYYY') as id, 0 as leaf from tab_plan_week group by to_char(planweekstart,'YYYY') order by id");

		// 维护时根节点
		} else if (week.length() == 4) {
			listTree = myService.findListBySql("select to_char(planweekstart,'MM') as text, '" + week + "' || '-' || to_char(planweekstart,'MM') as id, 0 as leaf  from tab_plan_week where to_char(planweekstart,'YYYY') = '" + week + "' group by to_char(planweekstart,'MM') order by id");
		} else {
			listTree = myService.findListBySql("select to_char(planweekstart,'DD') || '~' || to_char(planweekend,'DD') as text, id as id, 1 as leaf from tab_plan_week where to_char(planweekstart,'YYYY-MM') = '" + week + "' order by id");
		}
			
        JsonConfig jsonConfig = JsonUtils.configJson(new String[] {}, "yyyy-MM-dd");
    	
        JSON json = JSONSerializer.toJSON(listTree, jsonConfig);
        
        super.getResponse().getWriter().print(json.toString());
	}
	
	/**
	 * 获得左侧树
	 */
	public String toWeekTree() {
		
		List<HashMap<String, Object>> l = myService.findListBySql("select id as weekid, to_char(planweekstart, 'YYYY-MM-DD') as currentday from tab_plan_week where TO_CHAR(planweekstart, 'yyyy-MM-dd') <= TO_CHAR(sysdate, 'yyyy-MM-dd') and TO_CHAR(planweekend, 'yyyy-MM-dd') >= TO_CHAR(sysdate, 'yyyy-MM-dd') ");
		
		currentday = l.get(0).get("currentday").toString();
		weekid = l.get(0).get("weekid").toString();
		
		return "TREE_SUCCESS";
	}

	public String getBz() {
		return bz;
	}

	public String getIsAdmin() {
		return isAdmin;
	}

	public String getWeek() {
		return week;
	}

	public void setWeek(String week) {
		this.week = week;
	}

	public String getCurrentday() {
		return currentday;
	}

	public String getWeekid() {
		return weekid;
	}

	public void setWeekid(String weekid) {
		this.weekid = weekid;
	}
	
	

}
